<?php
namespace common\bonus;

use common\models\BalanceType;
use common\models\StaticInstance;
use common\models\StaticInstanceOrder;
use common\models\TradeModel;
use common\utils\CommonFun;
use common\bonus\Validata;


use common\models\finance\StaticInstanceOrderModel;

/*
	静态释放实例创建
	# 如果是按天固定值释放 最好是各自分开
*/
class StaticRelease
{

	/*
		* static_type 1 自己投资的 2动态奖励的  default 1
		300美金2.4倍
		500美金2.5倍
		1000美金2.6倍
		3000美金2.7倍
		5000美金2.8倍
		10000美金3倍
	*/
	public static $investArr = array(
		'300'	=> 2.4,
		'500'	=> 2.5,
		'1000'	=> 2.6,
		'3000'	=> 2.7,
		'5000'	=> 2.8,
		'10000' => 3,
	);


	public static function bonusAction($params)
	{

		$conf = TradeModel::getStaticReleaseConf('StaticRelease');
		if(!$conf) return ['state'=>false,'msg'=>'参数配置有误'];
		if($params['money']<=0) return ['state'=>false,'msg'=>'金额有误'];


		$staticType = isset($params['static_type'])?$params['static_type']:1;

		if($staticType==1)
		{
			//自己投资的
			return self::invsetInstance($params,$conf);

		}else if($staticType==2)
		{
			//动态奖励的
			return self::dynamicInstance($params,$conf);
		}
		return true;
	}


	## 自己投资的
	protected static function invsetInstance($params,$conf)
	{
		//实例 合并方式
		$instanceType = $conf['instance_type'];

		$where = ['mch_id'=>MCHID];
		$andArr = [];

		if($instanceType == 1)
		{
			//多条合并成一条
			$where['status'] = '1';
			$where['member_id'] = $params['member_id'];

			$model = StaticInstance::find()->where($where)->one();
		}else if($instanceType == 2)
		{
			//每天不同订单成一条
			$where['status'] = '1';
			$where['member_id'] = $params['member_id'];
			$where['commodity_id'] = $params['commodity_id'];
			$st = mktime(0,0,0,date('m'),date('d'),date('Y'));

			$and = ['>=','create_time',$st];	
			$model = StaticInstance::find()->where($where)->andWhere($and)->one();

		}else
		{
			$model = false;
		}

		$params['total_num'] = $params['money'];
		$params['in_num']    = $params['money'];
		$params['type']      = 1;
		$params['in_type']   = 1;
		//静态实例增加源投资数量UPH
        $fromPrice = BalanceType::getOne(['type' => 1], ['price'])['price'];
        $toPrice = BalanceType::getOne(['type' => 2], ['price'])['price'];
        $toTotal = round($params['money'] * $fromPrice / $toPrice, 2);
        $params['source_num'] = $toTotal;

		self::addInstance($model,$params,$conf);
	}

	## 动态奖励的
	protected static function dynamicInstance($params,$conf)
	{

		//实例 合并方式
		$instanceType = $conf['instance_type'];
		$where = ['mch_id'=>MCHID];
		$andArr = [];
		$params['in_type'] = 1;
		if($instanceType == 1)
		{
			//多条合并成一条
			$where['status'] = '1';
			$where['member_id'] = $params['member_id'];

			if($conf['dynamic_type'] == 1){
				//动态与静态分开
				$where['type'] = 2;
				$params['in_type'] = 2;
			}
			$model = StaticInstance::find()->where($where)->one();

		}else if($instanceType == 2)
		{
			//每天合并成一条
			$where['status'] = '1';
			$where['member_id'] = $params['member_id'];
			
			$st = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$and = ['>=','create_time',$st];	

			if($conf['dynamic_type'] == 1){
				//动态与静态分开
				$where['type'] = 2;
				$params['in_type'] = 2;
			}
			$model = StaticInstance::find()->where($where)->andWhere($and)->one();
		}else
		{
			$model = false;
		}		
		$params['total_num'] = $params['money'];
		$params['in_num'] = 0;
		$params['type'] = 2;
		self::addInstance($model,$params,$conf);
	}



	//添加实例
	private static function addInstance($insModel,$params,$conf)
	{

		//更新实例
		if(isset($insModel->id) && $insModel->id>0)
		{
			$insModel->in_num += $params['in_num'];
			$insModel->total_num += $params['total_num'];
			$insModel->remain_num += $params['total_num'];
			$insModel->status = '1';
			$insModel->update_time = time();
			$flag = $insModel->save();
			if(!$flag) return false;
			$insId = $insModel->id;
		}else
		{
			//添加
			$data = array();
			$data['member_id']  = $params['member_id'];
			$data['model_id']   = $conf['model_id'];
			$data['mch_id']     = MCHID;
			$data['in_num']     = $params['in_num'];
			$data['total_num']  = $params['total_num'];
			$data['remain_num'] = $params['total_num'];
			$data['status']     = '1';
			$data['create_time'] = time();
			$data['update_time'] = time();
			$data['type']        = $params['in_type'];
			$data['order_type']  = $params['order_type'];
			$data['commodity_id']   = $params['commodity_id'];

			if ($data['order_type']<>1) {
                /*签约周期天数*/
                $data['period_day'] = $params['period'];
                if ($params['order_type']==3 && $params['period']==0){
                    $data['period_day'] = 100;
                }
            }

		    $data['bonus_num'] = 0 ;
			$insModel = new StaticInstance();
			if(!$insModel->load($data,'') || !$insModel->save()) return false;
			$insId = \Yii::$app->db->getLastInsertID();
		}

		//添加实例日志
		$order = array();
		$order['member_id']     = $params['member_id'];
		$order['mch_id']        = MCHID;
		$order['instance_id']   = $insId;
		$order['type']          = $params['type'];
		$order['in_num']        = $params['in_num'];
		$order['total_num']     = $params['total_num'];
		$order['from_member_id'] = $params['type']==1?$params['member_id']:$params['from_member_id'];
		$order['source_id']      = $params['source_id'];
		$order['create_time']    = time();


		$insOrderModel = new StaticInstanceOrder();
		if(!$insOrderModel->load($order,'') || !$insOrderModel->save()){

//			 var_dump($insOrderModel->errors);
			return false;
		}

		return true;  
	}


}